////////////////////////////////////////////////////////////////////////////////
//
//  FLEX USER GROUP HOKURIKU
//  Copyright 2009 Flex User Group Hokuriku
//  All Rights Reserved.
// 
//  NOTICE: Flex User Group Hokuriku permits you to use, 
//  modify, and distribute this file 
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package net.fxug.hokuriku.chocolat.helper
{
    import flash.events.Event;
    import flash.events.MouseEvent;

    import mx.controls.Alert;
    import mx.core.IMXMLObject;
    import mx.events.FlexEvent;

    import net.fxug.hokuriku.chocolat.events.SocketEvent;
    import net.fxug.hokuriku.chocolat.events.ViewChangeEvent;
    import net.fxug.hokuriku.chocolat.events.VoteEvent;
    import net.fxug.hokuriku.chocolat.service.SocketClient;
    import net.fxug.hokuriku.chocolat.service.data.ConnectInfo;
    import net.fxug.hokuriku.chocolat.utils.FilterUtil;
    import net.fxug.hokuriku.chocolat.view.VoteControl;

    /**
     * Good/Badコントロールのヘルパークラス
     *
     * @author shoito
     */
    public class VoteControlHelper implements IMXMLObject
    {
        private var view:VoteControl;

        private var socketClient:SocketClient;
        private var name:String;

        public function initialized(document:Object, id:String):void
        {
            this.view = document as VoteControl;
            this.view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
            this.view.filters = [FilterUtil.getDropShadowFilter()];
        }

        private function creationCompleteHandler(event:FlexEvent):void
        {
            this.view.good.addEventListener(MouseEvent.CLICK, good_clickHandler);
            this.view.bad.addEventListener(MouseEvent.CLICK, bad_clickHandler);
            this.view.back.addEventListener(MouseEvent.CLICK, back_clickHandler);

            this.socketClient = ConnectInfo.socketClient;
            this.socketClient.addEventListener(SocketEvent.SERVER_CONNECT_CLOSE, this.socketCloseEventHandler);
            this.name = ConnectInfo.name;
        }

        private function resetComment():void
        {
            this.view.comment.text = "";
            this.view.comment.setFocus();
        }

        private function good_clickHandler(event:MouseEvent):void
        {
            var comment:String = view.comment.text;
			this.view.comment.errorString = "";

            this.socketClient.sendMessage(comment, VoteEvent.GOOD_EVENT, this.name);

            resetComment();
        }

        private function bad_clickHandler(event:MouseEvent):void
        {
            var comment:String = view.comment.text;
			if(comment.length == 0)
			{
				this.view.comment.errorString = "「ぶぅ」送信時にはコメントを入力してください。";
				return;
			}
			else
			{
				this.view.comment.errorString = "";
			}

            this.socketClient.sendMessage(comment, VoteEvent.BAD_EVENT, this.name);
            resetComment();
        }

        private function socketCloseEventHandler(event:SocketEvent):void
        {
            Alert.show("スピーカーとの接続を確立できませんでした。スピーカーとの接続が切れたため設定画面に戻ります。", "確認", 0, null, this.socketCloseAfter);
        }

        private function socketCloseAfter(event:Event):void
        {
            ConnectInfo.callBack();
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.BACK));
        }

        private function back_clickHandler(event:Event):void
        {
            ConnectInfo.callBack();
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.BACK));
        }
    }
}